package cc.bookcloud.tool.ocr.controller;

import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSON;
import cc.bookcloud.tool.ocr.common.JResult;
import cc.bookcloud.tool.ocr.common.enum1.SystemInfoEnum;
import cc.bookcloud.tool.ocr.common.requ.SubSystemInfoVersionVo;
import cc.bookcloud.tool.ocr.common.sqlite.ReleaseCodePackageEntity;
import cc.bookcloud.tool.ocr.common.sqlite.Sqlite3Client;
import cc.bookcloud.tool.ocr.common.version.VersionManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@Api(tags = "一体化编译发版辅助小工具", value = "一体化编译发版辅助小工具")
@RequestMapping("/release")
public class ReleaseController {


    @Resource
    private Sqlite3Client sqlite3Client;
    @Resource
    private VersionManager versionManager;


    @ApiOperation(value = "代码编译发版")
    @PostMapping("/compile")
    public JResult compile(@RequestBody List<SubSystemInfoVersionVo> versionVoList) {
        JResult jResult = JResult.build();
        log.info("接收到参数：" + JSON.toJSONString(versionVoList));
        try {
            for (SubSystemInfoVersionVo versionVo : versionVoList) {
                if (versionVo.isSelected()) {
                    //保存
                    ReleaseCodePackageEntity release = new ReleaseCodePackageEntity();
                    String identify = versionVo.getIdentify();
                    String version = versionVo.getVersion();
                    release.setAppIdentify(identify);
                    release.setAppName(SystemInfoEnum.getName(identify));
                    release.setAppVersion(version);
                    release.setRemark("--");
                    release.setYear("2022");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date now = new Date();
                    String nowStr = sdf.format(now);
                    release.setCreateTime(nowStr);
                    Entity entity = Entity.create("RELEASE_CODE_PACKAGE").parseBean(release, true, true);

                    int id = sqlite3Client.use().insert(entity);
                    if (id > 0) {
                        // 更新版本管理器
                        versionManager.updateVersion(identify, version);
                    }
                    log.info(" 插入ID:" + id);
                }
            }
            jResult.ok("成功");
        } catch (SQLException e) {
            jResult.failed("保存发版数据发生异常");
            log.error("保存发版数发生异常!", e);
        }
        return jResult;
    }

    @ApiOperation(value = "查询发版列表")
    @GetMapping("/query/list")
    public JResult<ReleaseCodePackageEntity> query() {
        JResult jResult = JResult.build();
        try {
            String sql = "select * from  RELEASE_CODE_PACKAGE order by id desc limit 100";
            List list = sqlite3Client.use().query(sql, ReleaseCodePackageEntity.class, null);
            jResult.ok(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jResult;
    }

    @ApiOperation(value = "删除发版记录")
    @PostMapping("/delete")
    public JResult deleteRelease(@RequestBody String[] subSystems) {
        JResult jResult = JResult.build();
        log.info("接收到参数：" + JSON.toJSONString(subSystems));

        /**
         * 插入数据库中，保存发版记录
         */
        for (int i = 0; i < subSystems.length; i++) {
            Entity entity = Entity.create("RELEASE_CODE_PACKAGE").set("ID", subSystems[i]);
            try {
                int id = sqlite3Client.use().del(entity);
                log.info(" 删除ID:" + id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return jResult.ok("删除成功");
    }


    @ApiOperation(value = "查询子系统及版本信息")
    @GetMapping("/query/version/all")
    public JResult<SubSystemInfoVersionVo> queryMaxVersion() {
        JResult jResult = JResult.build();
        List<SubSystemInfoVersionVo> list = new ArrayList<>();
        for (SystemInfoEnum item : SystemInfoEnum.values()) {
            if ("app".equalsIgnoreCase(item.getType())) {
                SubSystemInfoVersionVo version = new SubSystemInfoVersionVo();
                version.setIdentify(item.getIdentify());
                version.setName(item.getName() + " [" + item.getIdentify() + "]");
                String vStr = versionManager.fetchVersion(item);
                version.setVersion(vStr);
                list.add(version);
            }
        }
        return jResult.ok(list);
    }

}
